package com.kk.base.controller.home;

import com.kk.base.bean.CodeMsg;
import com.kk.base.bean.Result;
import com.kk.base.entity.home.*;
import com.kk.base.service.home.ComplaintService;
import com.kk.base.service.home.ExpressMailOrderService;
import com.kk.base.util.SessionUtil;
import com.kk.base.util.ValidateEntityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @info :  前台用户投诉控制层
 */
@RequestMapping("/home/complaint")
@Controller
public class HomeComplaintController {


    @Autowired
    private ComplaintService complaintService;
    @Autowired
    private ExpressMailOrderService expressMailOrderService;

    /**
     * 跳转到个人中心首页
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(Model model) {
        HomeUser homeUser = SessionUtil.getHomeUser();
        model.addAttribute("homeUser", homeUser);
        model.addAttribute("index", 7);
        model.addAttribute("homeUserTypeEnum", HomeUserTypeEnum.values());
        return "home/complaint/index";
    }


    /**
     * 用户投诉
     * @param complaint
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> add(Complaint complaint) {
        HomeUser homeUser = SessionUtil.getHomeUser();
        CodeMsg validate = ValidateEntityUtil.validate(complaint);
        if (validate.getCode() != CodeMsg.SUCCESS.getCode()) {
            return Result.error(validate);
        }
        if (expressMailOrderService.findByWaybillNumber(complaint.getWaybillNumber()) == null){
            return Result.error(CodeMsg.WAYBILLNUMBER_NO_EXIST);
        }
        if (complaintService.findByHomeUser_IdAndWaybillNumber(complaint.getWaybillNumber())!= null){
            return Result.error(CodeMsg.COMPLAINT_WAYBILLNUMBER_EXIST);
        }
        complaint.setHomeUser(homeUser);
        if (complaintService.save(complaint) == null) {
            return Result.error(CodeMsg.COMPLAINT_ERROR);
        }
        return Result.success(true);
    }



}
